<?php
namespace app\service;

use think\facade\Db;

class CategoryService
{
    public static function getTreeList($param=null)
    {
        $param['where']=isset($param['where'])?$param['where']:[['status','=',1]];
        $where_parent=['parent_id'=>0];
        if(isset($param['parent_id']))
        {
            $where_parent=['parent_id'=>$param['parent_id']];
        }
        $where_parent['country_id']=cache('lang_id');
        $param['table']=isset($param['table'])?$param['table']:'category';
        $data = Db::name($param['table'])->where($where_parent)->where($param['where'])->order('sort asc')->select()->toArray();
        $list=array();
        $spl='　├　';
        if(!empty($param['spl'])){
            $spl = $param['spl'];
        }
        if(count($data)==0)
        {
            if(isset($param['parent_id']))
            {
            $data=Db::name($param['table'])->where(['id'=>$param['parent_id'],'country_id'=>cache('lang_id')])->where($param['where'])->order('sort asc')->select()->toArray();
            }
        }
        foreach($data as &$v)
        {
            $v['spl']='';
            array_push($list,$v);
            $menu=Db::name($param['table'])->where(['parent_id'=>$v['id']])->where($param['where'])->order('sort asc')->select()->toArray();
            foreach($menu as &$vv)
            {
                $vv['spl']=$spl;
                array_push($list,$vv);
                $menu2=Db::name($param['table'])->where(['parent_id'=>$vv['id']])->where($param['where'])->order('sort asc')->select()->toArray();      
                foreach($menu2 as &$vvv)
                {
                    $vvv['spl']=$spl.$spl;
                    array_push($list,$vvv);
                }
            }
        }
       
        return $list;
    }
    public static function childId($param=null)
    {
        return Db::name('category')->where($param['where'])->column('id');
    
    }
    public static function datalist($param=null)
    {
        $where=array();
        if(!empty($param['where']))
        {
            $where=$param['where'];
        }
        $datalist = Db::name($param['table'])->where($where)->order('sort asc')->select()->toArray();
        return $datalist;
    }
    public static function all($param=null)
    {
        $where=array();
        if(!empty($param['where']))
        {
            $where=$param['where'];
        }
        $datalist = Db::name('ProductCategory')->where($where)->order('sort asc,id desc')->select()->toArray();
        return $datalist;
    }
    public static function infoByTag($param)
    {
        $data= Db::name('category')->where($param['where'])->find();
        if(!$data)
        {
            $menu=Db::name('menu')->where($param['where'])->find();
            if(!$menu)
            {
                return [];
            }
            $data=['name'=>$menu['title'],'sign'=>$menu['sign']];
            $category_id=Db::name('category')->insertGetId($data);
            $data['id']=$category_id;
        }
        return $data;
    }
    public static function info($param)
    {
        $data= Db::name('category')->where($param['where'])->find();        
        return $data;
    }
}